import axios from '@/config/axios/index.ts';
import { VxeUI } from '@/config/vxe/index.ts';
import type { JsonResultProps } from '#/JsonResult.d.ts';
import type { Dict, Dictionary } from '#/Dictionary.d.ts';
export const loadDict = async (type: Array<string>): Promise<Dict> => {
	return new Promise((resolve, reject): void => {
		axios<any, JsonResultProps<Array<Dictionary>>, any>({
			baseURL : '/api',
			method  : 'POST',
			url     : '/dict',
			data    : type
		}).then((jsonResult: JsonResultProps<Array<Dictionary>>): void => {
			if (jsonResult['status']) {
				const dict: Dict = {};
				const data: Array<Dictionary> = jsonResult.data!;
				const flag: boolean = (data && Array.isArray(data) && data.length) as boolean;
				if (flag) {
					jsonResult.data?.forEach((item: Dictionary): void => {
						dict[item.type] = item['dictionary'];
					});
				}
				resolve(dict);
			} else {
				VxeUI.modal.message({
					content : '字典加载失败',
					status  : 'warning',
					id      : 'failedMessage'
				});
				reject(jsonResult.msg);
			}
		}).catch((error: unknown) => {
			console.error(error);
			VxeUI.modal.message({
				content : '网络异常,请稍后重试',
				status  : 'warning',
				id      : 'failedMessage'
			});
			reject(error);
		});
	});
};